package polimi.sc.expoquest.storage.datasources;

import java.util.ArrayList;
import java.util.Collection;

import polimi.sc.expoquest.storage.DBContract;
import polimi.sc.expoquest.storage.DBHelper;
import polimi.sc.expoquest.storage.model.GameOption;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * This class is in charge of managing all the database
 * operations related to the OPTION TABLE, using the 
 * database helper, which does the direct calls to the 
 * database
 * SHOULD BE ONE FOR EACH AVAILABLE TABLE IN THE DATABASE 
 * @author Monica Lozano
 *
 */
public class GameOptionDataSource {
	//---------------------
	//ATTRIBUTES
	//------------------
	/**
	 * The object that represents the database
	 */
	private SQLiteDatabase db;
	/**
	 * The database helper for accessing the database 
	 */
	private DBHelper dbHelper;
	
	//----------------
	// CONSTRUCTOR
	//----------------
	public GameOptionDataSource(Context ctx)
	{
		//1. We create the DB helper
		dbHelper = new DBHelper(ctx);
	}
	
	//------------------
	//METHODS
	//------------------
	//utilities
	
	/**
	 * Creates the Option object from the data in the current
	 * row of the cursor.
	 * @param cursor
	 * @return
	 */
	private GameOption cursorToOption(Cursor cursor)
	{
		GameOption o = new GameOption();
		//1. We obtain the columns indexes based on their names
		int idColIndex = cursor.getColumnIndex(DBContract.Option._ID);
		int optColIndex = cursor.getColumnIndex(DBContract.Option.CLM_NAME_TEXT);
		//2.We obtain the data from the row based on the index
		int id =  cursor.getInt(idColIndex);
		String text = cursor.getString(optColIndex);
		//3.We save the data in the object
		o.setId(id);
		o.setOption(text);
		return o;
	}
	
	
	//database connection
	/**
	 * Opens the database such that we can used it for reading and writing
	 */
	public void open(){
		/* 
		 * TODO put this call in a asynchronous task, such that if it takes more than
		 * few seconds our application wont colapse.
		 */
		dbHelper.open();
		db = dbHelper.getWritableDatabase();
	}
	/**
	 * Close the database connection
	 */
	public void close()
	{
		dbHelper.close();
	}
	
	//------------------------
	// RETRIEVING INFORMATION
	//------------------------
	
	/**
	 * Returs an option given its id
	 * @param optionID
	 * @return
	 */
	public GameOption getOptions(int optionID)
	{
		//1. Doing the consult
		Cursor cursor = db.query(DBContract.Option.TABLE_NAME,
								DBContract.Option.ALL_COLUMNS,
								DBContract.Option._ID+ "="+ optionID,
								null,null,null,null);
		//2. We create the list from the results of the query
		cursor.moveToFirst();
		GameOption o = cursorToOption(cursor);
		cursor.close();
		return o;
	}
	
	
	
	/**
	 * Returs all the options available in the system
	 * @return
	 */
	public Collection<GameOption> getAllOptions()
	{
		Collection<GameOption> opts = new ArrayList<GameOption>();
		//1. Doing the consult
		Cursor cursor = db.query(DBContract.Option.TABLE_NAME,DBContract.Option.ALL_COLUMNS,
								null,null,null,null,null);
		//2. We create the list from the results of the query
		cursor.moveToFirst();
		while(!cursor.isAfterLast())
		{
			GameOption o = cursorToOption(cursor);
			opts.add(o);
			cursor.moveToNext();
		}
		cursor.close();
		return opts;
	}
	
	/**
	 * Inserts an option to the table
	 * @param option
	 * @return
	 */
	public GameOption insert(String option)
	{
		//1.We create the values for saving them into the database
		ContentValues values = generateValues(option);
		//2. We save it on the database
		long insertID= db.insert(DBContract.Option.TABLE_NAME, null, values);
		//3. We return the alarm object saved succesfully into the database
		Cursor cursor = db.query(DBContract.Option.TABLE_NAME, 
								DBContract.Option.ALL_COLUMNS, 
								DBContract.Option._ID+ "="+ insertID, 
								null, null, null, null);
		//3.1. Move the cursor to recent inserted row
		cursor.moveToFirst();
		//3.2. Creating the Alarm object
		GameOption o = cursorToOption(cursor);
		//3.3 Closing the cursor
		cursor.close();
		return o;
	}
	/**
	 * Create the values for query
	 * @param opt
	 * @return
	 */
	private ContentValues generateValues(String opt)
	{
		ContentValues values = new ContentValues();
		values.put(DBContract.Option.CLM_NAME_TEXT, opt);
		return values;
	}
	/**
	 * Deletes an option
	 * @param t
	 * @return
	 */
	public boolean delete(GameOption o)
	{
		long id = o.getId();
		int total= db.delete(DBContract.Option.TABLE_NAME,
				  DBContract.Option._ID+"="+id, null);
		return total>0;
	}
}
